﻿using Microsoft.Data.Sqlite;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace SqliteConnectionWithSqlSugar
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private SqlSugarClient _db;

        public MainWindow()
        {
            InitializeComponent();
        }

        // 选择路径按钮点击事件
        private void btn_selectPath_Click(object sender, RoutedEventArgs e)
        {
            var dialog = new Microsoft.Win32.OpenFileDialog
            {
                Filter = "SQLite Database files (*.db)|*.db",
                Title = "选择SQLite数据库文件"
            };
            if (dialog.ShowDialog() == true)
            {
                txt_path.Text = dialog.FileName;
                InitializeDatabase(txt_path.Text);
            }
        }

        // 初始化数据库连接
        private void InitializeDatabase(string dbPath)
        {
            string password = "cm4s3kdf1hu5we";
            string connString = new SqliteConnectionStringBuilder
            {
                DataSource = dbPath,
                //Mode = SqliteOpenMode.ReadWriteCreate,
                Password = password
            }.ToString();

            // 配置 SqlSugarClient
            _db = new SqlSugarClient(new ConnectionConfig
            {
                ConnectionString = connString,
                DbType = SqlSugar.DbType.Sqlite,
                IsAutoCloseConnection = true
            });

            // 确保数据库已连接并创建表
            try
            {
                _db.DbMaintenance.CreateDatabase(); // 如果不存在数据库则创建
                _db.CodeFirst.InitTables<Users>(); // 初始化用户表
                txtInfo.Text = "数据库连接成功并且表已创建。\n";
            }
            catch (Exception ex)
            {
                txtInfo.Text = $"数据库初始化失败：{ex.Message}\n";
            }
        }

        // 插入样例数据
        private void btn_insert_Click(object sender, RoutedEventArgs e)
        {
            if (_db == null)
            {
                txtInfo.Text = "请先选择数据库并初始化连接。\n";
                return;
            }

            try
            {
                var user = new Users { Name = "张三", Age = 25 };
                _db.Insertable(user).ExecuteCommand();
                txtInfo.Text += $"插入用户：{user.Name}, 年龄：{user.Age}\n";
            }
            catch (Exception ex)
            {
                txtInfo.Text = $"插入数据失败：{ex.Message}\n";
            }
        }

        // 查询样例数据
        private void btn_query_Click(object sender, RoutedEventArgs e)
        {
            if (_db == null)
            {
                txtInfo.Text = "请先选择数据库并初始化连接。\n";
                return;
            }

            try
            {
                var users = _db.Queryable<Users>().ToList();
                txtInfo.Text += "查询结果：\n";
                foreach (var user in users)
                {
                    txtInfo.Text += $"ID: {user.Id}, Name: {user.Name}, Age: {user.Age}\n";
                }
            }
            catch (Exception ex)
            {
                txtInfo.Text = $"查询数据失败：{ex.Message}\n";
            }
        }
    }

    // 用户表的模型类
    public class Users
    {
        [SugarColumn(IsPrimaryKey = true, IsIdentity = true)] // 设置主键和自增列
        public int Id { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
    }
}
